package com.yaxin.lucene;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.lucene.analysis.morfologik.MorfologikAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.QueryBuilder;

public class Lucene {

	public static void main(String[] args) throws IOException{
		RAMDirectory directory = new RAMDirectory();  
		LocalFile(directory);
		
		//HDFS(directory);
		
        //IndexReader reader = new IndexReader();  
        IndexReader reader = DirectoryReader.open(directory);    
        IndexSearcher searcher = new IndexSearcher(reader);   
        //searcher.setSimilarity(new IKSimilarity());  
        String keyWords = "cuit";  
        MorfologikAnalyzer analyzer = new MorfologikAnalyzer(); 
        QueryBuilder qp = new QueryBuilder(analyzer);
//        Query query = qp.createPhraseQuery("name", keyWords);
        Query query = new TermQuery(new Term("name", keyWords));
        //Query query = qp.parse(keyWords);  
        TopDocs topDocs = searcher.search(query, Integer.MAX_VALUE);  
        ScoreDoc[] docs = topDocs.scoreDocs;
        for(ScoreDoc doc : docs){
        	Document d = searcher.doc(doc.doc);
        	System.out.println(d.get("contents"));
        }
	}
	
	
	public static void HDFS(RAMDirectory directory) throws IOException{
		String dst = "hdfs://192.168.10.153:9000/hdfs/test/qq.txt";
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(URI.create(dst), conf);
		FSDataInputStream hdfsInStream = fs.open(new Path(dst));
		//DataInputStream d = new DataInputStream(hdfsInStream);
		BufferedReader reader =new BufferedReader(new InputStreamReader(hdfsInStream));
		//创建IKAnalyzer中文分词对象  
		MorfologikAnalyzer analyzer = new MorfologikAnalyzer(); 
		IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
		// 使用智能分词  
        //analyzer.setUseSmart(true); 
		IndexWriter writer = new IndexWriter(directory,iwc);
		//FileReader read = new FileReader("D:\\index"+"part-00000");
		//BufferedReader br = new BufferedReader(read);
		String row;
		Document doc =new Document();
	    while((row = reader.readLine())!=null){
	    	doc.add(new TextField("contents",row,Field.Store.YES));  
			writer.addDocument(doc);
	    }
		writer.close();
		reader.close();
		hdfsInStream.close();
		fs.close();
	}
	
	
	public static void LocalFile(RAMDirectory directory) throws IOException{
		//创建IKAnalyzer中文分词对象  
		MorfologikAnalyzer analyzer = new MorfologikAnalyzer(); 
		//analyzer.setUseSmart(true); 
		IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
		IndexWriter writer = new IndexWriter(directory,iwc);
		FileReader read = new FileReader("D:\\test.txt");
		BufferedReader br = new BufferedReader(read);
		String row;
		Document doc;
		String[] cels;
		while((row = br.readLine())!=null){
			doc =new Document();
			doc.add(new TextField("contents",row,Field.Store.YES));  
			cels = row.split(",");
			doc.add(new TextField("time",cels[0],Field.Store.YES));  
			doc.add(new TextField("name",cels[1],Field.Store.YES));  
			doc.add(new TextField("group",cels[2],Field.Store.YES));  
			writer.addDocument(doc);
		}
		writer.close();
		br.close();
		read.close();
		
	}
}
